<template>
    <div>
        <div class="crumbs">
            <el-breadcrumb separator="/">
                <el-breadcrumb-item>
                    <i class="el-icon-lx-calendar"></i> 药品采购
                </el-breadcrumb-item>
                <el-breadcrumb-item>采购信息</el-breadcrumb-item>
            </el-breadcrumb>
        </div>
        <div class="container">
            <div class="handle-box">
                <el-input v-model="query.name" placeholder="请输入采购员名称" class="handle-input mr10"></el-input>
                <el-button type="primary" icon="el-icon-search" @click="handleSearch">搜索</el-button>
            </div>
            <el-table :data="tableData" border class="table" ref="multipleTable" header-cell-class-name="table-header">
                <el-table-column prop="bill.billId" label="编号" width="50" align="center"></el-table-column>
                <el-table-column prop="supplierName" label="名称"></el-table-column>
                <el-table-column prop="doctorName" label="采购人员"></el-table-column>
                <el-table-column prop="bill.allQuantity" label="药品总量"></el-table-column>
                <el-table-column prop="bill.allPrice" label="药品总价"></el-table-column>
                <el-table-column prop="bill.createTime" label="添加时间"></el-table-column>
                <el-table-column prop="bill.updateTime" label="修改时间"></el-table-column>
                <el-table-column label="操作" width="180" align="center">
                    <template #default="scope">
                        <el-button type="text" icon="el-icon-more" @click="handleDetail(scope.$index, scope.row)">详情
                        </el-button>
<!--                        <el-button type="text" icon="el-icon-delete" class="red"-->
<!--                            @click="handleDelete(scope.$index, scope.row)">删除</el-button>-->
                    </template>
                </el-table-column>
            </el-table>
            <div class="pagination">
                <el-pagination background layout="total, prev, pager, next" :current-page="query.current"
                    :page-size="query.pageSize" :total="query.total" @current-change="handlePageChange"></el-pagination>
            </div>
        </div>

        <!-- 详情弹出框 -->
        <el-dialog title="详情" v-model="detailVisible" width="30%">
            <el-table :data="detailData" border class="table" ref="multipleTable" header-cell-class-name="table-header">
                <el-table-column prop="billIntroId" label="编号" width="50" align="center"></el-table-column>
                <el-table-column prop="medicineName" label="药品名称"></el-table-column>
                <el-table-column prop="quantity" label="药品数量"></el-table-column>
                <el-table-column prop="price" label="药品单价"></el-table-column>
            </el-table>
            <template #footer>
                <span class="dialog-footer">
                    <el-button type="primary" @click="detailVisible = false">关 闭</el-button>
                </span>
            </template>
        </el-dialog>
    </div>
</template>

<script>
import { onActivated, ref, reactive, inject } from "vue";
import { ElMessage, ElMessageBox } from "element-plus";
import { getBillList, getBillDetail } from "../../api";

export default {
    name: "bill",
    setup() {
        const query = reactive({
            current: 1,
            pageSize: 10,
            total: 0,
            name: '',
        });
        const tableData = ref([]);
        const pageTotal = ref(0);
        const reload  = inject("reload");
        // 获取表格数据
        const getData = () => {
            getBillList(query).then((res) => {
                tableData.value = res.data.message;
                query.total = res.data.total;
            });
        };
        getData();

        // 查询操作
        const handleSearch = () => {
            query.current = 1;
            getData();
        };
        // 分页导航
        const handlePageChange = (val) => {
            query.current = val;
            getData();
        };

        // 删除操作
        const handleDelete = (index, row) => {
            // 二次确认删除
            ElMessageBox.confirm("确定要删除吗？", "提示", {
                type: "warning",
            }).then(() => {
                // deleteSupplier(row.supplierId).then(res => {
                //     if (res.code === 200) {
                //         reload();
                //         ElMessage.success('删除成功');
                //     } else {
                //         console.log("[删除供应商] 失败: " + res.message);
                //         ElMessage.error('服务器繁忙，请稍后重试');
                //     }
                // })
            }).catch(() => {});
        };

        // 详情表格弹窗
        const detailVisible = ref(false);
        let detailData = ref([]);

        const handleDetail = (index, row) => {
            console.log("[Bill] handleDetail");
            getBillDetail(row.bill.billId).then(res => {
                if (res.code === 200) {
                    detailData.value = res.data.billIntro;
                    detailVisible.value = true;
                } else {
                    console.log("[获取采购详情] 失败: " + res.message);
                    ElMessage.error('服务器繁忙，请稍后重试');
                }
            })
        };

        onActivated(() => {
            console.log("Bill onActivated!");
            getData();
        });

        return {
            query,
            tableData,
            pageTotal,
            detailVisible,
            detailData,
            handleSearch,
            handlePageChange,
            handleDelete,
            handleDetail,
        };
    },
};
</script>

<style scoped>
.handle-box {
    margin-bottom: 20px;
}
.handle-input {
    width: 300px;
    display: inline-block;
}
.table {
    width: 100%;
    font-size: 14px;
}
.red {
    color: #ff0000;
}
.mr10 {
    margin-right: 10px;
}
</style>
